﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wallpaper.Models.Entities;

namespace Wallpaper.Data.Configurations
{
    public class DownloadRecordConfigation : IEntityTypeConfiguration<DownloadRecord>
    {
        public void Configure(EntityTypeBuilder<DownloadRecord> builder)
        {
            // 设置表名
            builder.ToTable("download_records");

            // 配置主键
            builder.HasKey(d => d.Id);

            builder.Property(d => d.Id)
                .HasMaxLength(36)
                .IsRequired()
                .HasComment("下载记录主键ID");

            // 用户ID配置
            builder.Property(d => d.UserId)
                .HasMaxLength(36)
                .IsRequired()
                .HasComment("下载用户ID");

            // 壁纸ID配置
            builder.Property(d => d.WallpaperId)
                .HasMaxLength(36)
                .IsRequired()
                .HasComment("被下载的壁纸ID");

            // 消耗积分配置
            builder.Property(d => d.PointsCost)
                .IsRequired()
                .HasDefaultValue(1) // 默认消耗1积分
                .HasComment("本次下载消耗的积分");

            // 下载时间配置
            builder.Property(d => d.CreatedAt)
                .IsRequired()
                .HasDefaultValueSql("CURRENT_TIMESTAMP")
                .HasComment("下载时间");


            // 创建复合索引，提高查询性能
            builder.HasIndex(d => d.UserId)
                .HasDatabaseName("idx_downloadrecords_userid");

            builder.HasIndex(d => d.WallpaperId)
                .HasDatabaseName("idx_downloadrecords_wallpaperid");


            builder.HasIndex(d => d.CreatedAt)
                .HasDatabaseName("idx_downloadrecords_createdat");


        }
    }
}
